import { PagingMessage, YScrollableContainer } from './../../../components/scrollable/y-scrollable-container';
import { Observable } from 'rxjs';
import { Confirm } from './../../../components/common-components/confirm';
import { ConfirmMessageResult } from './../../../components/messageBox/confirmMessage';
import { GrowlMessageType } from './../../../components/messageBox/growMeaageType';
import { MessageBoxService } from './../../../components/messageBox/messageBoxService';
import { LocalstorageDataManage } from './../../../common/localstoreage/localstoreageDataManage';
import { Employee } from './../../../models/Employee';
import { EmployeeInfosManage, EmployeInfo, EmployeInfoStatus } from './../../../services/employeesInfoManage';
import { NavigateManage } from './../../../common/navigateManage';
import { Component, OnInit, ViewChild, AfterViewInit } from '@angular/core';
import { employeesService } from './../../../services/employee';
import * as R from "ramda";

@Component({
    selector: 'deliver-turn',
    templateUrl: './deliverTurn.component.html',
    styleUrls: ['./deliverTurn.component.scss']
})

export class DeliverTurnComponent implements OnInit,AfterViewInit {

    @ViewChild(YScrollableContainer)
    private yScrollableContainer: YScrollableContainer;
    private nextCount;
    private previousCount;

    private isAdd = false; //是否有添加按钮
    private turnName; // 员工班次名称
    private turns: Array<{ turn: string, employees: Array<Employee> }> = [];
    private turnsEmployees = [];
    private employees: Array<Employee>;
    private editTurnName;

    checkboxValues;
    checkedValues: Array<string>;
    async ngOnInit() {
        await Observable.timer(500)
            .timeInterval()
            .pluck('interval')
            .take(1).first().toPromise()
        this.comboData();
        this.getTurns();
    }

    ngAfterViewInit(){
        
    }

    async comboData() {
        this.employees = await employeesService.getDishesDistributor();
        const employeeInfos = EmployeeInfosManage.current.getEntities();
        this.employees.forEach(employee => {
            const employeeInfo = employeeInfos.find(employeeInfo => employeeInfo.id == employee.id);
            employee.status = employeeInfo && employeeInfo.status;
            employee.turns = employeeInfo && employeeInfo.turns;
        })
        this.checkboxValues = this.employees.map(x => {
            return {
                id: x.id,
                label: x.user.name
            }
        })
    }

    onChange(checkedValues) {
        this.checkedValues = checkedValues;
    }
    goTurnOpe(ope, turn?) {
        this.isAdd = true;
        setTimeout(()=>{
            this.yScrollableContainer.childrenChanged();
        },500)
        if (ope == 'add') {
            this.turnName = "";
            // this.checkedValues = this.employees.filter(x => x.status == EmployeInfoStatus.online).map(x => x.id)
            this.checkedValues = this.employees.map(x => x.id);
            // console.log('checkedValues:',this.checkedValues);
        }

        if (ope == 'edit') {
            const employees = EmployeeInfosManage.current.getEmployeeInfosByTurn(turn);
            this.turnName = turn;
            this.editTurnName = turn;
            this.checkedValues = employees.filter(employee => employee.turns.find(turnName => turnName == turn)).map(x => x.id);
        }
    }

    async saveTurn() {
        if (!this.turnName) {
            MessageBoxService.current.Growl(GrowlMessageType.Error, '班次名称不能为空');
            return false;
        }
        if (!this.checkedValues || !this.checkedValues.length) {
            MessageBoxService.current.Growl(GrowlMessageType.Error, '请选择该班次员工');
            return false;
        }

        if (this.editTurnName != this.turnName) {
            EmployeeInfosManage.current.renameTurn(this.turnName, this.editTurnName);
        }

        EmployeeInfosManage.current.saveEmployeInfosTurns(this.turnName, this.checkedValues);

        this.isAdd = false;
        this.getTurns();
    }

    async getTurns() {
        this.turns = [];
        const allTurns = EmployeeInfosManage.current.getTurns().filter(x => x != '全部');
        const employees: Array<Employee> = await employeesService.getDishesDistributor();
        allTurns.forEach(turn => {
            this.turns.push({
                turn: turn,
                employees: EmployeeInfosManage.current.getEmployeeInfosByTurn(turn).map(employeeInfo => employees.find(employee => employee.id == employeeInfo.id))
            })
        });
    }

    getName(employees) {
        let employeeInfos = [];
        employees.forEach(employee => { employeeInfos.push(employee.user.name) });
        return employeeInfos;
    }

    async removeTurns(turn) {
        const result = await MessageBoxService.current.Show("删除确认", "确认要删除该员工班次？");
        if (result == ConfirmMessageResult.Ok) {
            const employeeInfos = EmployeeInfosManage.current.removeTurn(turn);
            this.getTurns();
        }
    }

    pagingInfo: PagingMessage
    pagingMessageChange(event) {
        console.log("2222", event)
        this.pagingInfo = event;
        this.nextCount = this.pagingInfo.nextCount;
        this.previousCount = this.pagingInfo.previousCount;
    }
}